#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string reverseOnlyLetters(string s) {
        int left = 0;
        int right = s.size() - 1;
        while (left < right)
        {
            while (!isalpha(s[left]) && left < right)
            {
                ++left;
            }
            while (!isalpha(s[right]) && left < right)
            {
                --right;
            }
            if (left < right)
            {
                swap(s[left], s[right]);
                ++left;
                --right;
            }
        }
        return s;
    }
};